// pages/songDetail/songDetail.js
import request from '../../utils/request.js'
import PubSub from 'pubsub-js'
import moment from 'moment'
var appInstance = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay:false,
    song:{},
    musicId:'',
    musicLink:'',
    currentTime: '00:00',
    totalTime:'00:00',
    currentWidth:''

  },

  //切换歌曲
  handleSwitchSong(event){
  let type = event.currentTarget.id;
  this.manager.stop();

    PubSub.subscribe("musicId",(msg,musicId)=>{
      this.getSongById(musicId);
      //自动播放
      this.handleMusicControll(musicId,true);

      PubSub.unsubscribe("musicId");

    })

  //发送消息给推荐歌曲
  PubSub.publish("switchType",type);
  },

//点击播放按钮，触发该事件
  handlePlay(){
    this.setData({
      isPlay : !this.data.isPlay
    })
    let { musicId, isPlay, musicLink} = this.data;
    this.handleMusicControll(musicId, isPlay, musicLink);
 
  },

  //控制音乐暂停 播放
  async handleMusicControll(musicId, isPlay, musicLink){

    if(isPlay){
      if(!musicLink){
        //获取音乐播放地址
        let song = await request("/song/url", { id: musicId });
        musicLink = song.data[0].url;

        this.setData({
          musicLink
        })
      }
       this.manager.src=musicLink;
       this.manager.title=this.data.song.name;

       this.manager.play();
    }else{
      this.manager.pause();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    this.manager = wx.getBackgroundAudioManager();
   //获取musicId;
   this.setData({
     musicId: options.musicId,
     index:options.index
   })
    
    this.getSongById(this.data.musicId);
//进来先判断 ，这首音乐有没有播放过
if(appInstance.globalData.isPlay && appInstance.globalData.musicId ===this.data.musicId){
  this.setData({
    isPlay: true
  })
  this.manager.play();
}
    //监听音乐播放，停止，结束
    this.manager.onPlay(()=>{
      this.setData({
        isPlay: true
      })
      appInstance.globalData.isPlay=true;
      appInstance.globalData.musicId=this.data.musicId;

    });

    this.manager.onPause(() => {
      this.setData({
        isPlay: false
      })
      appInstance.globalData.isPlay = true;
    });
    this.manager.onStop(() => {
      this.setData({
        isPlay: false
      })

      appInstance.globalData.isPlay = false;
    });

    //监听音乐自动结束
    this.manager.onEnded(()=>{
      // 切换下一首
      PubSub.publish('switchType','xia');

      //将进度条为0
      this.setData({
        currentWidth:0,
        currentTime:'00:00'
      })
    })


//订阅消息
    PubSub.subscribe("musicId", (msg, musicId) => {
      this.getSongById(musicId);
      //自动播放
      this.handleMusicControll(musicId, true);

      PubSub.unsubscribe("musicId");

    })


    // 监听音乐实时时间改变
    this.manager.onTimeUpdate(()=>{
      let currentTime = moment(this.manager.currentTime*1000).format("mm:ss");
      let currentWidth = this.manager.currentTime/this.manager.duration*450;
      this.setData({
        currentTime,
        currentWidth
      })
    })

 
  
  
  },

  //动态获取某首歌曲
  async getSongById(musicId){
    let song = await request("/song/detail", { ids: musicId });

    
    let totalTime = moment(song.songs[0].dt).format("mm:ss");
    this.setData({
      song: song.songs[0],
      totalTime
    });

    //动态设置导航名称
    wx.setNavigationBarTitle({
      title: this.data.song.name
    
    })

   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})